﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace FormAuktionsHus
{
    class ClsClientHandler
    {
        Socket clientSocket;

        NetworkStream netStream;
        StreamReader reader;
        StreamWriter writer;
        ClsAuktionMonitor auktionMonitor;

        public ClsClientHandler(Socket ClientSocket, ClsAuktionMonitor auktionMonitor)
        {
            this.clientSocket = ClientSocket;
            this.auktionMonitor = auktionMonitor;                   
        }

        // Modtager bud
        public void Run()
        {
            netStream = new NetworkStream(clientSocket);
            reader = new StreamReader(netStream);
            writer = new StreamWriter(netStream);

            writer.WriteLine(auktionMonitor.GetVarernavn() +"|"+ auktionMonitor.GetVareStartpris().ToString() +"|"+"Nuværende bud: "+ auktionMonitor.GetVareNuværendePris.ToString());
            writer.Flush();
            auktionMonitor.TilføjKlient(netStream);

            try
            {
                while (true)
                {
                    string bud = reader.ReadLine();
                    long.Parse(bud);
                    auktionMonitor.ModtagBud(bud, clientSocket);
                }
            }

            catch
            {
                
            }            
        } 
    }
}
